//
//  QuickSearchResultViewController.swift
//  ddx0513
//
//  Created by shuijing on 15/8/16.
//  Copyright (c) 2015年 jiang yongbin. All rights reserved.
//

import UIKit

class QuickSearchResultViewController: UITableViewController {

    
    //@property(nonatomic,weak)NSString *strTtile;
    
    @IBOutlet weak var searchNum: UILabel!
    var dataArr:NSMutableArray = []
    var imageArr:NSMutableArray = []
    var hitArr:NSMutableArray = []
    var lessonidArr:NSMutableArray = []
    var courseArr = NSArray()
    var itemList:AllCourseItem! = nil
    var totalcount = 0
    var lineImageView:UIImageView! = nil
    //var searchString:String! = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        searchResult()
       // filterString = "1"

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    
    func searchResult() {
        var userid:Int = 1
       // var keyword:String = filterString!
        var keyword:String = GlobalData.searchstring
        NetUtils.netRequest(Method.GET, URLString: NetUtils.getURLLessonList() , parameters: ["keyword":keyword], responseHandler: nil, netErrorHandler: nil, successHandler: searchResultHandle, interErrorHandler: nil, jsonErrorHandler: nil)
    }
    func searchResultHandle(json: JSON) {
        
        totalcount = json["totalcount"].int!
        dataArr.removeAllObjects()
        for (var i = 0;i < totalcount; i++ )
        {
            dataArr.addObject(json["list"][i]["lessonname"].string!)
            imageArr.addObject(json["list"][i]["imgurl"].string!)
            
            hitArr.addObject(json["list"][i]["hit"].int!)
            lessonidArr.addObject(json["list"][i]["lessonid"].int!)
            
        }
        self.tableView.reloadData()
        
    }
    
    func baseResult(str: String){
        var userid:Int = 1
        var keyword:String = str
        NetUtils.netRequest(Method.GET, URLString: NetUtils.getURLLessonList() , parameters: ["keyword":keyword], responseHandler: nil, netErrorHandler: nil, successHandler: searchResultHandle, interErrorHandler: nil, jsonErrorHandler: nil)
        
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //return visibleResults.count
        return dataArr.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //return tableView.dequeueReusableCellWithIdentifier("AllCourseItem2", forIndexPath: indexPath) as! UITableViewCell NSLog("******************\(tableView)tb")
        
        searchNum.text = "共找到\(totalcount)个相关内容"
        var tableCell :AllCourseItem = tableView.dequeueReusableCellWithIdentifier("AllCourseItem2") as! AllCourseItem
        var s = dataArr.objectAtIndex(indexPath.row) as? String
        
        tableCell.courseName.text = s
        tableCell.starImage.image = UIImage(named: "星级.png")
        var str = hitArr.objectAtIndex(indexPath.row) as! Int
        tableCell.numOfPeople.text = "\(String(str))人在学"
        tableCell.accessoryType = UITableViewCellAccessoryType.None
        var url:String = imageArr.objectAtIndex(indexPath.row) as! String
        
        
        DBUtils.getImageFromFile(url, successHandler: { (imgUrl, img) -> Void in
            if (url == imgUrl) {
                tableCell.courseImage.image = img
            }
        })
        return tableCell
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        //cell.textLabel!.text = visibleResults[indexPath.row]
        // cell.textLabel!.text = allResults1[indexPath.row]
    }
   
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        
        //tableView.deselectRowAtIndexPath(indexPath, animated: true)
        var allcoursedetail = UIStoryboard(name: "AllCourse", bundle: nil).instantiateViewControllerWithIdentifier("AllCourseDetail") as! AllCourseDetail
        rootController.navigationController?.pushViewController(allcoursedetail, animated: true)
        allcoursedetail.isFirstPage = false
        
        //        self.presentViewController(allcoursedetail, animated: true, completion: nil)
        //// var lessonid = lessonidArr.objectAtIndex(indexPath.row) as! Int
        allcoursedetail.lessonId =  lessonidArr.objectAtIndex(indexPath.row) as? Int

        // var lessonName = dataArr.objectAtIndex(indexPath.row) as? String
        allcoursedetail.lessonName = dataArr.objectAtIndex(indexPath.row) as! String
       
    }

    
    @IBAction func goBack(sender: AnyObject) {
        
        self.navigationController?.popViewControllerAnimated(true)
      //  rootController.navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
